#include <SFML/Graphics.hpp>
#include "Unit.hpp"

#include <iostream>


Unit::Unit()
{
    m_life = 10;
    m_max_life = 10;
    m_attack = 0;
    m_movement_speed = 0;
    m_current_move = 0;
    m_range = 0;
    m_isSelected = false;
    m_isHovered = false;
    m_isDead = false;
    m_pos = sf::Vector2i(-1, -1);
    m_owner = NULL ;
    m_current_attack_count = 1 ;
    m_attack_count = 1 ;
}


Unit::Unit(int attack, int life, int mov, int range, Player* owner)
{
    m_life = life;
    m_max_life = life;
    m_attack = attack;
    m_movement_speed = mov;
    m_current_move = mov;
    m_range = range;
    m_isSelected = false;
    m_isHovered = false;
    m_isDead = false;
    m_pos = sf::Vector2i(-1, -1);
    m_owner = owner ;
    m_current_attack_count = 1 ;
    m_attack_count = 1 ;
}

MoveCase* Unit::GetCaseFromPos(int x, int y){
    for (int i(0); i<m_availableCase.size(); i++)
    {
        if ( m_availableCase[i]->getX() == x && m_availableCase[i]->getY() == y )
            return m_availableCase[i] ;
    }
    return NULL ;
}

int Unit::GetIdFromPos(int x, int y) const {
    for (int i(0); i<m_availableCase.size(); i++)
    {
        if ( m_availableCase[i]->getX() == x && m_availableCase[i]->getY() == y )
            return i ;
    }
    return -1 ;
}

bool Unit::takeDamage(Unit * attacker)
{
    m_life -= attacker->getAttack() ;
    if (m_life <= 0) // Ded
    {
        m_isDead = true ;
        return true ; // return ded
    }
    else if (m_life > m_max_life)
    {
        m_life = m_max_life ;
    }

    return false ; // return not_ded

}

bool Unit::takeDamage(Unit * attacker, int dmg)
{
    m_life -= dmg ;
    if (m_life <= 0) // Ded
    {
        m_isDead = true ;
        return true ; // return ded
    }
    else if (m_life > m_max_life)
    {
        m_life = m_max_life ;
    }

    return false ; // return not_ded

}

void Unit::dealDamage(Unit * target)
{
    target->takeDamage(this);
}

void Unit::dealDamage(Unit * target, int dmg)
{
    target->takeDamage(this, dmg);
}

void Unit::setPosition(sf::Vector2i new_pos)
{
    m_pos = new_pos;
}


void Unit::setPosition(int x, int y)
{
    m_pos = sf::Vector2i(x,y);
}

bool Unit::isInRange(Unit* target, int range )
{
    if (range == -1)
        range = getRange() ;
    if (target != NULL)
    {
        sf::Vector2i vec = (getPos() - target->getPos());
        return abs(vec.x) + abs(vec.y) <= range;
    }
    else
        return false;
}

void Unit::resetMove()
{
    m_current_move = m_movement_speed ;
    m_current_attack_count = m_attack_count ;
}

void Unit::setSelect(bool s)
{
    m_isSelected = s;
}

void Unit::setHovered(bool s)
{
    m_isHovered = s;
}

void Unit::setDead(bool s)
{
    m_isDead = s;
}

void Unit::draw(Drawing* drawingmodule, sf::Color outlinecolor)
{
    int c_size = Constants::CASE_SIZE ;
    sf::RectangleShape unit(sf::Vector2f(c_size, c_size));



    unit.setFillColor(m_owner->getInsideColor());





    unit.setPosition(m_pos.x*c_size, m_pos.y*c_size);

    if (!m_isDead)
    {

        drawingmodule->getWindow()->draw(unit);
        sf::Text text(convertInt(getLife()),drawingmodule->getFont(0), 25);
        if ( getLife() < getMaxLife() )
            text.setColor(sf::Color(220,20,60));
        drawingmodule->textOnCase(getPos().x , getPos().y , text);
        drawingmodule->overLineUnit(getPos().x , getPos().y , m_owner->getOutsideColor());

        if (m_isSelected)
            drawingmodule->shaderRange(m_pos.x,m_pos.y,0,sf::Color::White,m_owner->getOutsideColor()) ;
    }




    /*if (m_isHovered)
    {
        drawingmodule->shaderRange(m_pos.x,m_pos.y,0,sf::Color(50,50,50,255),sf::Color(50,50,50,150)) ;
    }*/
}
